package wens.pig.base.plugin.form;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDBizException;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:校验是否为温氏集团
 *
 * @author yaord
 * @date 18:34 2022/7/21
 */
public class OrgCheckFormPlugin extends AbstractFormPlugin {

    private static final List<String> LIST = new ArrayList<>();

    static {
        LIST.add("refresh");
        LIST.add("modify");
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate option = (FormOperate) args.getSource();
        String key = option.getOperateKey();
        long orgId = RequestContext.get().getOrgId();
        DynamicObject object = BusinessDataServiceHelper.loadSingle(orgId, "bos_org");
        String name = object.getString("name");
        if (!LIST.contains(key)){
            if (!"温氏集团".equals(name)) {
                throw new KDBizException("“请切换到温氏集团下进行新增、删除、启用、禁用操作”");
            }
        }
    }
}
